package com.tianxiao.faas.biz.service.corner;

import com.tianxiao.faas.api.param.corner.BasePageParam;
import com.tianxiao.faas.api.param.corner.CustomInfoResult;
import com.tianxiao.faas.api.param.corner.CustomInfoSaveParam;
import com.tianxiao.faas.common.util.DateUtils;
import com.tianxiao.faas.common.util.PageUtils;
import com.tianxiao.faas.mapper.dao.CustomInfoModelMapper;
import com.tianxiao.faas.mapper.model.CustomInfoModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomInfoService {

    @Resource
    private CustomInfoModelMapper customInfoModelMapper;

    public void save(CustomInfoSaveParam param, Long userId) {
        CustomInfoModel customInfoModel = new CustomInfoModel();
        long currentTimeMillis = System.currentTimeMillis();
        customInfoModel.setUserId(userId);
        customInfoModel.setShopName(param.getShopName());
        customInfoModel.setShopAddress(param.getShopAddress());
        customInfoModel.setRemark(param.getRemark());
        customInfoModel.setCreated(currentTimeMillis);
        customInfoModel.setUpdated(currentTimeMillis);
        customInfoModel.setDeleted(0);
        customInfoModelMapper.insertSelective(customInfoModel);

    }

    public List<CustomInfoResult> list(BasePageParam param) {
        List<CustomInfoModel> customInfoModels = customInfoModelMapper.list(PageUtils.getOffset(param.getPage(), param.getPageSize()), param.getPageSize());
        List<CustomInfoResult> customInfoResults = new ArrayList<>();

        for (CustomInfoModel customInfoModel : customInfoModels) {
            CustomInfoResult customInfoResult = getCustomInfoResult(customInfoModel);
            customInfoResults.add(customInfoResult);
        }

        return customInfoResults;
    }

    public Integer count() {
        Integer count = customInfoModelMapper.count();
        return count == null ? 0 : count;
    }


    private CustomInfoResult getCustomInfoResult(CustomInfoModel customInfoModel) {
        CustomInfoResult customInfoResult = new CustomInfoResult();
        customInfoResult.setId(customInfoModel.getId());
        customInfoResult.setUserId(customInfoModel.getUserId());
        customInfoResult.setShopName(customInfoModel.getShopName());
        customInfoResult.setShopAddress(customInfoModel.getShopAddress());
        customInfoResult.setRemark(customInfoModel.getRemark());
        customInfoResult.setCreateTime(DateUtils.format(customInfoModel.getCreated()));
        customInfoResult.setUpdateTime(DateUtils.format(customInfoModel.getUpdated()));
        return customInfoResult;
    }
}
